package com.azirkxs.clothes.cys.service.impl;

import com.alibaba.fastjson2.JSON;
import com.azirkxs.clothes.cys.entity.User;
import com.azirkxs.clothes.cys.mapper.UserMapper;
import com.azirkxs.clothes.cys.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author azirkxs
 * @since 2023-04-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Map<String, Object> login(User user) {
        //根据用户名和密码查询
        LambdaQueryWrapper<User> queryMapper = new LambdaQueryWrapper<>();
        queryMapper.eq(User::getUsername,user.getUsername());
        queryMapper.eq(User::getPassword,user.getPassword());
        User loginUser = this.baseMapper.selectOne(queryMapper);
        //结果不为空，则生成token,并且将用户信息放入redis

        if(loginUser!=null){
            //生成token,jwt
            String key = "user"+UUID.randomUUID();
            //存入redis
            loginUser.setPassword(null);
            redisTemplate.opsForValue().set(key,loginUser,30, TimeUnit.MINUTES);
            //返回数据
            Map<String,Object> data  = new HashMap<>();
            data.put("token",key);
            return data;
        }

        return null;
    }

    @Override
    public Map<String, Object> getUserInfo(String token) {
        //根据token获取用户信息
        Object obj = redisTemplate.opsForValue().get(token);
        if(obj !=null){
            User loginUser = JSON.parseObject(JSON.toJSONString(obj), User.class);
            Map<String,Object> data = new HashMap<>();
            data.put("userId",loginUser.getUserId());
            data.put("name",loginUser.getUsername());
            data.put("email",loginUser.getEmail());
            data.put("gender",loginUser.getGender());
            data.put("avatar",loginUser.getAvatar());
            data.put("birthday",loginUser.getBirthday());
            System.out.println(loginUser.getBirthday());

            //用户喜好标签

            // SELECT * FROM user_tags a,tags b WHERE a.tag_id = b.tag_id AND a.user_id = 1
            System.out.println(this.baseMapper.getTagNameByUserId((loginUser.getUserId())));
            System.out.println("打印查到了什么玩意");
            List<Map> tagsList = this.baseMapper.getTagNameByUserId((loginUser.getUserId()));
            data.put("tags",tagsList);

            List<Map> armoireList = this.baseMapper.getArmoireByUserId(loginUser.getUserId());
            data.put("armoire",armoireList);

            return data;

        }
        return null;
    }

    @Override
    public Map<String, Object> getPersonalReocommend(String token) {
        //根据token获取用户信息
        Object obj = redisTemplate.opsForValue().get(token);
        if(obj != null){
            User loginUser = JSON.parseObject(JSON.toJSONString(obj), User.class);
            List<Map> clothesList = this.baseMapper.getPersonalReocommend(loginUser.getUserId());
            Map<String,Object> data = new HashMap<>();
            data.put("personal",clothesList);
            return data;
        }

        return null;
    }

    @Override
    public void logout(String token) {
        redisTemplate.delete((token));
    }

    @Override
    public void register(User user) throws BusinessException {
        // 查询用户名是否存在
        User userExist = this.baseMapper.getUserByUsername(user.getUsername());
        if (userExist != null) {
            throw new BusinessException("用户名已存在");
        }

        // 插入用户
        this.baseMapper.insertUser(user);
    }

    @Override
    public void infoEdit(User user) {
        this.baseMapper.infoEdit(user);
    }

    @Override
    public int changePassword(String newPassword,String oldPassword, Integer userId) {
        Map<String,Object> data = new HashMap<>();
        data.put("newPassword",newPassword);
        data.put("oldPassword",oldPassword);
        data.put("userId",userId);
        return  baseMapper.changePassword((data));
    }


}

